package me.zhengjie.modules.system.service.impl;

import me.zhengjie.modules.system.domain.ProjectSetting;
import me.zhengjie.utils.ValidationUtil;
import me.zhengjie.modules.system.repository.ProjectSettingRepository;
import me.zhengjie.modules.system.service.ProjectSettingService;
import me.zhengjie.modules.system.service.dto.ProjectSettingDTO;
import me.zhengjie.modules.system.service.mapper.ProjectSettingMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import java.util.Optional;

/**
* @author 张鹏
* @date 2019-05-06
*/
@Service
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class ProjectSettingServiceImpl implements ProjectSettingService {

    @Autowired
    private ProjectSettingRepository projectSettingRepository;

    @Autowired
    private ProjectSettingMapper projectSettingMapper;

    @Override
    public ProjectSettingDTO findById(Long id) {
        Optional<ProjectSetting> projectSetting = projectSettingRepository.findById(id);
        ValidationUtil.isNull(projectSetting,"ProjectSetting","id",id);
        return projectSettingMapper.toDto(projectSetting.get());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ProjectSettingDTO create(ProjectSetting resources) {
        return projectSettingMapper.toDto(projectSettingRepository.save(resources));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(ProjectSetting resources) {
        Optional<ProjectSetting> optionalProjectSetting = projectSettingRepository.findById(resources.getId());
        ValidationUtil.isNull( optionalProjectSetting,"ProjectSetting","id",resources.getId());

        ProjectSetting projectSetting = optionalProjectSetting.get();
        // 此处需自己修改
        resources.setId(projectSetting.getId());
        projectSettingRepository.save(resources);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(Long id) {
        projectSettingRepository.deleteById(id);
    }
}